const xlsx = require('node-xlsx')
const zh = require('../../src/locales/zh.json')
const en = require('../../src/locales/en.json')
const { forEach } = require('lodash')

const generateExcelData = () => {
  let str = 'key||中文||English\n'
  forEach(en, (v, k) => {
    if (v === '') {
      str += 'key' + '||' + zh[k] + '||' + '1 \n'
    }
  })
  return str
}

async function exportExcel (ctx) {
  const str = generateExcelData()
  const newWorkSheet = []

  const data = [['key', '1', '2']]
  // str.split('\n').forEach(row => {
  //   const item = row.split('||')
  //   data.push(item)
  // })

  newWorkSheet.push({ name: 'sheet1', data: data })
  // wch 字符宽度
  const options = {
    type: 'binary'
  }
  const buffer = xlsx.build(newWorkSheet, options)
  ctx.body = buffer
}

module.exports = router => {
  router.post('/exportExcel', exportExcel)
}